package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.SocProgItemAddDTO;
import org.jsola.hr.dto.SocProgItemBatchDTO;
import org.jsola.hr.dto.SocProgItemUpdateDTO;
import org.jsola.hr.query.SocProgItemQuery;
import org.jsola.hr.service.ISocProgItemService;
import org.jsola.hr.vo.SocProgItemListVO;
import org.jsola.hr.vo.SocProgItemVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 参保方案表子项
 *
 * @author zhr
 */
@RestController("hrSocProgItemController")
@RequestMapping(HrConstants.API_PATH + "/v1/socProgItems")
public class SocProgItemController implements ISocProgItemControllerAPI {

    @Autowired
    private ISocProgItemService socProgItemService;

    @Override
    @PostMapping
    public Result<SocProgItemVO> save(@Valid @RequestBody SocProgItemAddDTO socProgItemAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增参保方案表子项权限校验

        return Result.success(
                socProgItemService.save(socProgItemAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody SocProgItemUpdateDTO socProgItemUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改参保方案表子项权限校验

        int count = socProgItemService.update(socProgItemUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @PostMapping(value = "/batch")
    public Result batch(@Valid @RequestBody SocProgItemBatchDTO socProgItemBatchDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量操作权限校验

        // 批量操作，注意校验，有越权风险
        // 新增
        List<SocProgItemAddDTO> socProgItemAddDTOList = socProgItemBatchDTO.getSocProgItemAddDTOList();
        if (!CollectionUtils.isEmpty(socProgItemAddDTOList)) {
            socProgItemService.batchSave(socProgItemAddDTOList,tokenUser);
        }
        // 修改
        List<SocProgItemUpdateDTO> socProgItemUpdateDTOList = socProgItemBatchDTO.getSocProgItemUpdateDTOList();
        if (!CollectionUtils.isEmpty(socProgItemUpdateDTOList)) {
            socProgItemUpdateDTOList.parallelStream().forEach(
                    socProgItemUpdateDTO -> {
                        socProgItemService.update(socProgItemUpdateDTO,tokenUser);
                    }
            );
        }
        // 删除
        List<String> deleteSocProgItemIdList = socProgItemBatchDTO.getDeleteSocProgItemIdList();
        if (!CollectionUtils.isEmpty(deleteSocProgItemIdList)) {
            socProgItemService.deleteByIds(tokenUser,deleteSocProgItemIdList.toArray(new String[0]));
        }
        return Result.success();
    }


    @Override
    @DeleteMapping(value = "/{socProgItemId}")
    public Result<Integer> delete(@PathVariable String socProgItemId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除参保方案表子项权限校验

        int count = socProgItemService.deleteByIds(tokenUser, socProgItemId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] socProgItemIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除参保方案表子项权限校验

        int count = socProgItemService.deleteByIds(tokenUser, socProgItemIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{socProgItemId}")
    public Result<SocProgItemVO> findById(@PathVariable String socProgItemId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看参保方案表子项权限校验

        return Result.success(
            socProgItemService.selectById(socProgItemId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<SocProgItemListVO>> listAll(@Valid SocProgItemQuery socProgItemQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看参保方案表子项权限校验

        return Result.success(
            socProgItemService.select(socProgItemQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<SocProgItemListVO>> page(@Valid SocProgItemQuery socProgItemQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            socProgItemService.selectPage(socProgItemQuery, tokenUser.getSiteId())
        );
    }


}

